package com.jinzhi.jzweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.utils.ApiResult;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzapi.domain.UserDO;
import com.jinzhi.jzapi.utils.JWTUtil;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jinzhi.jzweb.dao.JazShippingAddressDao;
import com.jinzhi.jzweb.domain.JazShippingAddressDO;
import com.jinzhi.jzweb.service.JazShippingAddressService;
import com.jinzhi.common.base.CoreServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 用户收货地址表
 * </pre>
 * <small> 2024-05-13 16:49:56 | Long</small>
 */
@Service
public class JazShippingAddressServiceImpl extends CoreServiceImpl<JazShippingAddressDao, JazShippingAddressDO> implements JazShippingAddressService {

    private final UserService userService; //用户表

    public JazShippingAddressServiceImpl(UserService userService){
        this.userService = userService;
    }

    /**
     * 新增收货地址
     *
     * @param receivingName 姓名
     * @param phone         联系电话
     * @param province      省
     * @param city          市
     * @param district      区
     * @param address       详细地址
     * @return
     */
    @Override
    public Result<?> saveIntegralUserShippingAddress(String receivingName, String phone, String province, String city, String district, String address) {
        UserDO getUser = JWTUtil.getUser();
        UserDO userDO = userService.selectById(getUser.getId());

//        UserDO user = userOpenfeign.getUserById(1504759766907674626L);


        if (StringUtils.isEmpty(receivingName)) {
            return Result.fail("收货人姓名不能为空");
        }
        if (StringUtils.isEmpty(phone)) {
            return Result.fail("手机号不能为空");
        }
        if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city) || StringUtils.isEmpty(district) || StringUtils.isEmpty(address)) {
            return Result.fail("地址不能为空");
        }

        JazShippingAddressDO integralUserShippingAddress = new JazShippingAddressDO();
        integralUserShippingAddress.setReceivingName(receivingName);
        integralUserShippingAddress.setPhone(phone);
        integralUserShippingAddress.setProvince(province);
        integralUserShippingAddress.setCity(city);
        integralUserShippingAddress.setDistrict(district);
        integralUserShippingAddress.setAddress(address);
        integralUserShippingAddress.setUserId(Long.valueOf(userDO.getId()));
        integralUserShippingAddress.setCreateTime(new Date());
        integralUserShippingAddress.setIsDefault(0);
        integralUserShippingAddress.setIsDelete(0);
        return Result.ok(this.insert(integralUserShippingAddress));
    }

    /**
     * 查询指定用户的收货地址
     * @param current
     * @param size
     * @return
     */
    @Override
    public Result<?> selectByAddress(String current, String size) {
        long userId = JWTUtil.getUser().getId();
//        long userId = 1800352190213079042L;
        Page<JazShippingAddressDO> page = new Page<>(Integer.valueOf(current), Integer.valueOf(size));
        Wrapper<JazShippingAddressDO> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_delete", 0);
        return Result.ok(this.selectPage(page, queryWrapper));
    }

    /**
     * 查询指定用户的默认收货地址
     * @param userId
     * @return
     */
    @Override
    public Result<?> selectByIsDefaultAddress(String userId) {
        Wrapper<JazShippingAddressDO> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("user_id", userId);
        queryWrapper.eq("is_default", "1");
        queryWrapper.eq("is_delete", 0);
        return Result.ok(this.selectOne(queryWrapper));
    }

    /**
     * 设置默认地址
     * @param addressId
     * @param userId
     * @return
     */
    @Override
    public Result<?> setIsDefault(String addressId, String userId) {
        Wrapper<JazShippingAddressDO> queryWrapper = new EntityWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<JazShippingAddressDO> list = this.selectList(queryWrapper);
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getIsDefault() == 1) {
                list.get(i).setIsDefault(0);
                this.updateById(list.get(i));
            }
        }
        JazShippingAddressDO integralUserShippingAddress = this.selectById(addressId);
        integralUserShippingAddress.setIsDefault(1); //设置默认地址
        return Result.ok(this.updateById(integralUserShippingAddress));
    }

    /**
     * 删除指定地址
     * @param addressId
     * @return
     */
    @Override
    public Result<?> delAddress(String addressId) {
        JazShippingAddressDO integralUserShippingAddress = this.selectById(addressId);
        integralUserShippingAddress.setIsDelete(1);
        return Result.ok(this.updateById(integralUserShippingAddress));
    }

    /**
     *
     * @param addressId
     * @param receivingName
     * @param phone
     * @param province
     * @param city
     * @param district
     * @param address
     * @return
     */
    @Override
    public Result<?> updateAddress(String addressId, String receivingName, String phone, String province, String city, String district, String address) {
        if (StringUtils.isEmpty(receivingName)) {
            return Result.fail("收货人姓名不能为空");
        }
        if (StringUtils.isEmpty(phone)) {
            return Result.fail("手机号不能为空");
        }
        if (StringUtils.isEmpty(province) || StringUtils.isEmpty(city) || StringUtils.isEmpty(district) || StringUtils.isEmpty(address)) {
            return Result.fail("地址不能为空");
        }

        JazShippingAddressDO integralUserShippingAddress = new JazShippingAddressDO();
        integralUserShippingAddress.setId(Long.valueOf(addressId));
        integralUserShippingAddress.setReceivingName(receivingName);
        integralUserShippingAddress.setPhone(phone);
        integralUserShippingAddress.setProvince(province);
        integralUserShippingAddress.setCity(city);
        integralUserShippingAddress.setDistrict(district);
        integralUserShippingAddress.setAddress(address);
        return Result.ok(this.updateById(integralUserShippingAddress));
    }


}
